package baidu.hudson.plugin.result.collector.MessageRouter;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * this is the Message sneder builder , which will generate some default element for the message sender
 */
public class MessageSenderBuilder {
    private CamelContext context;
    private List<RouteBuilder> routes;
    private Map<String, Component> components;
    private static String defaultEndpoint = "tcp://db-testing-atd00.db01.baidu.com:8230";//"tcp://127.0.0.1:62626";
    //private static String defaultEndpoint = "tcp://127.0.0.1:62626";

    private MessageSenderBuilder() {
    }

    public static MessageSenderBuilder createMessageSenderBuilder() {
        return new MessageSenderBuilder();
    }


    public MessageSenderBuilder setContext(CamelContext context) {
        this.context = context;
        return this;
    }

    public MessageSenderBuilder setRoutes(List<RouteBuilder> routes) {
        this.routes = routes;
        return this;
    }

    public MessageSenderBuilder createRoute(RouteBuilder route) {
        if (null == getRoutes() || 0 == getRoutes().size()) {
            setRoutes(new ArrayList<RouteBuilder>());
        }
        getRoutes().add(route);

        return this;
    }

    public MessageSenderBuilder setComponents(Map<String, Component> components) {
        this.components = components;
        return this;
    }

    public MessageSenderBuilder createComponent(String componentName, Component component) {

        if (null == getComponents() || 0 == getComponents().size()) {
            Map<String, Component> components = new HashMap<String, Component>();
            setComponents(components);
        }
        getComponents().put(componentName, component);
        return this;
    }


    public MessageSenderBuilder createJmsComponent(String componentName, String brokerUrl) {

        if (null == getComponents() || 0 == getComponents().size()) {
            Map<String, Component> components = new HashMap<String, Component>();
            setComponents(components);
        }
        JmsComponent jmsComponent = new JmsComponent();
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerUrl);
        jmsComponent.setConnectionFactory(activeMQConnectionFactory);
        Executor executor = Executors.newCachedThreadPool();
        jmsComponent.setTaskExecutorSpring2(executor);
        getComponents().put(componentName, jmsComponent);
        return this;
    }


    public MessageSender build() {
        initContext();
        try {
            generateContext();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return MessageSender.createMessageSender(this);
    }

    private void initContext() {
        if (null == getContext()) {
            setContext(new DefaultCamelContext());
        }
        if (null == getRoutes() || 0 == getRoutes().size()) {
            setRoutes(new ArrayList<RouteBuilder>());
            getRoutes().add(new RouteBuilder() {
                @Override
                public void configure() throws Exception {

                }
            });
        }
        if (null == getComponents() || 0 == getComponents().size()) {
            JmsComponent jmsComponent = new JmsComponent();
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
            activeMQConnectionFactory.setBrokerURL(defaultEndpoint);
            jmsComponent.setConnectionFactory(activeMQConnectionFactory);
            Map<String, Component> component = new HashMap<String, Component>();
            component.put("jms", jmsComponent);
            setComponents(component);
        }
    }

    private void generateContext() throws Exception {
        for (RouteBuilder routeBuilder : getRoutes()) {
            getContext().addRoutes(routeBuilder);
        }
        for (String key : getComponents().keySet()) {
            getContext().addComponent(key, getComponents().get(key));
        }
    }

    public CamelContext getContext() {
        return context;
    }

    public List<RouteBuilder> getRoutes() {
        return routes;
    }

    public Map<String, Component> getComponents() {
        return components;
    }
}